package com.crawler.harbour.utils;


import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;

/** 
 * 解析properties工具类 
 */  
public class SettingReader {

	    /**
	     * 从相应的.properties文件中读取配置
	     * @param path .properties 配置文件的路径
	     * @param PropertyName  key
	     * @return the value to which the specified key is mapped, or null if this map contains no mapping for the key
	     */
	    public static String getValue(String path,String PropertyName){
	        Properties properties = new Properties();
            InputStream is = null;
            URL url = SettingReader.class.getClassLoader().getResource(path);
            try {  
                File file = new File(url.getFile().replaceAll("%20"," "));
                if(file.exists()) {
                    is =  new BufferedInputStream(new FileInputStream(file));//优选读取工程下配置文件
                }else{
                    is = SettingReader.class.getResourceAsStream("/"+path);//当前类加载器路径配置文件
                }
                properties.load(is);  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  finally {
                if (is != null) {  
                    try {  
                        is.close();
                    } catch (IOException e) {  
                        e.printStackTrace();  
                    }  
                }  
            }
            String propertyValue = (String) properties.get(PropertyName);  
            return propertyValue;  
	    }  
	    
	    /**
	     * 从默认配置文件中读取配置
	     * @param PropertyName  key
	     * @return the value to which the specified key is mapped, or null if this map contains no mapping for the key
	     */
	    public static String getBaseSettingValue(String PropertyName){
	    	
	    	return  getValue("dockSpider.properties", PropertyName);
	    }  


}
